
using System;
using System.Data;
using System.Data.SqlClient;

namespace CrmDocman.DataCore {
    /// <summary>
    /// Wrapper of sql adapter
    /// </summary>
    public class AdapterWrapper {

        #region SMethods
        /// <summary>
        /// Create a sql parameter
        /// </summary>
        /// <returns>IDBdata adapter</returns>
        public static IDbDataParameter CreateNewParameter() {
            return new SqlParameter();
        }

        /// <summary>
        /// Fill data table by sql data adapter
        /// </summary>
        /// <param name="table"></param>
        /// <param name="command"></param>
        public static void FillDataTable(DataTable table, IDbCommand command) {
            // new adapter
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            // set select command
            dataAdapter.SelectCommand = (SqlCommand) command;
            // fill table
            dataAdapter.Fill(table);
        }

        public static void FillDataTable(DataTable table, IDbCommand command,bool mapped) {
            // new adapter
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            // set select command
            if (mapped) {
                dataAdapter.TableMappings.Add("Table1", table.TableName);
            }
            dataAdapter.SelectCommand = (SqlCommand)command;
            // fill table
            //dataAdapter.Fill(table);
        }

        /// <summary>
        /// Update data table by sql data adapter
        /// </summary>
        /// <param name="table"></param>
        /// <param name="insertCmd"></param>
        /// <param name="updateCmd"></param>
        /// <param name="deleteCmd"></param>
        /// <returns></returns>
        public static int UpdateDataTable(DataTable table, IDbCommand insertCmd, IDbCommand updateCmd, IDbCommand deleteCmd) {
            // init data adapter
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.InsertCommand = (SqlCommand) insertCmd;
            dataAdapter.UpdateCommand = (SqlCommand) updateCmd;
            dataAdapter.DeleteCommand = (SqlCommand) deleteCmd;

            int result = dataAdapter.Update(table);
            return result;
        }
        #endregion SMethods

    }
}
